package com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.networkAssets.dict;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.networkassets.dict.ItNetworkAssetsDict;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.dict.ItNetworkAssetsDictDto;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ItNetworkAssetsDictRepo extends JpaRepository<ItNetworkAssetsDict, Integer> {

    @Query(value = "select count(*) from t_gw_it_net_assets_dict " +
            "where deleted = 0 " +
            "and code = :code " +
            "and if ( :id is not null, id != :id , 1 = 1 ) " +
            "", nativeQuery = true)
    Integer countByCodeAndIdNot(@Param("code") String code, @Nullable @Param("id") Long id);

    @Query(value = "select * from t_gw_it_net_assets_dict " +
            "where deleted = 0 " +
            "and if ( :#{#param.code} is not null , code like CONCAT('%',:#{#param.code},'%') , 1=1) " +
            "and if ( :#{#param.desc} is not null , describtion like CONCAT('%',:#{#param.desc},'%'), 1=1) " +
            "and if ( :#{#parentCodeIsNull} = true, parent_code is null , 1=1) " +
            "", nativeQuery = true)
    List<ItNetworkAssetsDict> findAllByParam(@Param("param") ItNetworkAssetsDictDto param, @Param("parentCodeIsNull") boolean parentCodeIsNull);

    ItNetworkAssetsDict findByCode(String code);

    void deleteAllByCodeIn(List<String> codeList);

    void deleteAllByIdIn(List<Long> delIdList);

    List<ItNetworkAssetsDict> findAllByParentCode(String parentCode);

    @Query(value = "select * from t_gw_it_net_assets_dict " +
            "where deleted = 0 " +
            "and id = :id " +
            "", nativeQuery = true)
    ItNetworkAssetsDict findById(@NonNull @Param("id") Long id);
}
